//
//  XBHomeCell.swift
//  Sina
//
//  Created by 周旭斌 on 2017/3/27.
//  Copyright © 2017年 周旭斌. All rights reserved.
//

import UIKit
import SDWebImage

class XBHomeCell: UITableViewCell {
    
    var iconImage : UIImageView?
    var titleLabel : UILabel?
    var contentLabel : UILabel?
    
    var model : XBHomeModel? {
        didSet {
            // 如果头像有值,就设置
            if var string = model?.img_url {
                string = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
                iconImage?.sd_setImage(with: URL.init(string: string), placeholderImage: nil);
            }
            // 如果有title就设置
            if let title = model?.title {
                titleLabel?.text = title;
            }
            if let content = model?.zhaiyao {
                contentLabel?.text = content;
            }
        }
    }
    
    // 类方法
    class func homeCellWith(_ tableView : UITableView) -> XBHomeCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "homeCell") as? XBHomeCell;
        if cell == nil {
            cell = XBHomeCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "homeCell");
        }
        return cell!;
    }
    
    // MARK:初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        
        // 在这里创建控件
        // 头像
        iconImage = UIImageView();
        iconImage!.backgroundColor = UIColor.red;
        contentView.addSubview(iconImage!);
        iconImage?.snp.makeConstraints({ (maker) in
            maker.left.top.equalTo(contentView).offset(8);
            maker.width.equalTo(XBWidth(40));
            maker.height.equalTo(XBHeight(40));
        })
        
        // 标题
        titleLabel = UILabel();
        titleLabel?.text = "测试数据";
        contentView.addSubview(titleLabel!);
        titleLabel?.snp.makeConstraints({ (maker) in
            maker.left.equalTo((iconImage?.snp.right)!).offset(XBWidth(8));
            maker.top.equalTo(contentView).offset(XBHeight(8));
            maker.width.equalTo(XBWidth(100));
            maker.height.equalTo(XBHeight(20));
        })
        
        // 内容label
        contentLabel = UILabel();
        contentLabel?.text = "我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容";
        contentLabel?.numberOfLines = 0;
        contentView.addSubview(contentLabel!);
        contentLabel?.snp.makeConstraints({ (maker) in
            maker.left.equalTo((titleLabel?.snp.left)!);
            maker.top.equalTo((titleLabel?.snp.bottom)!).offset(XBHeight(8));
            maker.right.bottom.equalTo(contentView).offset(XBWidth(-8));
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
